package cn.mcsys.myadmin.dubbo.service.impl;

import cn.mcsys.myadmin.common.service.ServiceException;
import cn.mcsys.myadmin.common.utils.GpsUtil;
import cn.mcsys.myadmin.common.utils.QrCodeUtils;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.dao.*;
import cn.mcsys.myadmin.modules.air.entity.*;
import cn.mcsys.myadmin.modules.air.service.*;
import cn.mcsys.myadmin.modules.air.vo.AirRidePointVO;
import cn.mcsys.myadmin.modules.mobile.dao.AirRealtimeUserCountDao;
import cn.mcsys.myadmin.modules.mobile.dao.AirRealtimeUserOriginDao;
import cn.mcsys.myadmin.modules.mobile.dao.AirUserFlowCountDao;
import cn.mcsys.myadmin.modules.mobile.entity.AirRealtimeUserCount;
import cn.mcsys.myadmin.modules.mobile.entity.AirRealtimeUserOrigin;
import cn.mcsys.myadmin.modules.mobile.entity.AirUserFlowCount;
import cn.mcsys.myadmin.modules.platform.dao.AirVehicleGpsBusDao;
import cn.mcsys.myadmin.modules.platform.dao.AirVehicleGpsTaxiDao;
import cn.mcsys.myadmin.modules.platform.entity.AirVehicleGpsBus;
import cn.mcsys.myadmin.modules.platform.entity.AirVehicleGpsTaxi;
import cn.mcsys.myadmin.modules.share.dao.AirOutsideCarDao;
import cn.mcsys.myadmin.modules.share.dto.AirCarDTO;
import cn.mcsys.myadmin.modules.share.entity.AirOutsideCar;
import cn.mcsys.myadmin.modules.viewer.dao.*;
import cn.mcsys.myadmin.modules.viewer.entity.*;
import cn.mcsys.myadmin.modules.viewer.service.AirETicketsService;
import cn.mcsys.myadmin.modules.viewer.service.impl.AirRideQRCodeServiceImpl;
import cn.mcsys.myadmin.modules.worker.dao.AirTaxiDao;
import cn.mcsys.myadmin.modules.worker.entity.AirTaxi;
import cn.mcsys.myadmin.modules.worker.vo.WorkerIndexVO;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class BackServiceImpl implements BackService {
    public static final String FILEDIR = "QRCodePicture";

    @Autowired
    private AirCarService airCarService;

    @Autowired
    private AirRidePointDao airRidePointDao;

    @Autowired
    private AirRidePointService airRidePointService;

    @Autowired
    private AirETicketsDao airETicketsDao;

    @Autowired
    private AirETicketsService airETicketsService;

    @Autowired
    private AirViewerUserDao airViewerUserDao;

    @Autowired
    private AirSendMessageService airSendMessageService;

    @Autowired
    private AirTaxiDao airTaxiDao;

    @Autowired
    private AirDriverService driverService;

    @Autowired
    private AirVehicleGpsBusDao airVehicleGpsBusDao;

    @Autowired
    private AirVehicleGpsTaxiDao airVehicleGpsTaxiDao;

    @Autowired
    private AirRideQrCodeDao airRideQrCodeDao;

    @Autowired
    private AirTicketsDao airTicketsDao;

    @Autowired
    private AirParkingLocationDao airParkingLocationDao;

    @Autowired
    private AirWorkerDao airWorkerDao;

    @Autowired
    private AirRealtimeMessageDao airRealtimeMessageDao;


    @Autowired
    private AirOutsideCarDao airOutsideCarDao;

    @Autowired
    private AirCarRecordDao airCarRecordDao;

    @Autowired
    private AirRideQrCodeSpecialDao airRideQrCodeSpecialDao;

    @Autowired
    private AirShuttlebusRecordDao airShuttlebusRecordDao;

    @Autowired
    private AirRealtimeUserCountDao airRealtimeUserCountDao;

    @Autowired
    private AirRealtimeUserOriginDao airRealtimeUserOriginDao;

    @Autowired
    private AirUserFlowCountDao airUserFlowCountDao;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private AirRealtimeMessageService airRealtimeMessageService;

    @Autowired
    private AirCarSumRecordDao airCarSumRecordDao;

    private static Logger logger = LoggerFactory.getLogger(AirRideQRCodeServiceImpl.class);

    @Override
    public List<AirCar> selectByPointId(Integer pointId) {
        List<AirCar> allList = airCarService.selectByPointId(pointId.intValue());
        allList = GpsUtil.changeCarListLngLat(allList);
        return allList;
    }

    @Override
    public String sayHi() {
        System.out.println("Hello World");
        return "Hello";
    }

    /**
     * 查询所有乘车点
     * @return
     */
    @Override
    public List<AirRidePoint> selectAllPoint() {
        return airRidePointService.findAllList();
    }

    @Override
    public List<AirRidePoint> selectAllPoint2() {
        return airRidePointService.findAllAppointedList();
    }

    /**
     * 通过乘车点ID查询对应的乘车点
     * @param id
     * @return
     */
    @Override
    public AirRidePoint selectPointById(Integer id) {
        return airRidePointService.get(id.intValue());
    }

    /**
     * 通过用户查询对应的电子票
     * @param id
     * @return
     */
    @Override
    public List<AirETickets> selectETicketsByUserId(Long id) {
        return airETicketsDao.selectByUserId(id);
    }

    /**
     * 根据证件的类型和号码查询对应的票务信息
     * @param type
     * @param number
     * @return
     */
    @Override
    public List<AirTickets> selectByTypeAndNumber(int type, String number) {
        return airTicketsDao.selectByTypeAndNumber(type, number);
    }

    /**
     * 保存电子票
     * @param airTickets
     * @param user
     */
    @Override
    public void saveETicket(List<AirTickets> airTickets, AirViewerUser user) {
        for (AirTickets airTicket : airTickets) {
            AirETickets airETickets = new AirETickets();
            airETickets.setTicketDate(airTicket.getTicketDate());
            airETickets.setTicketNumber(airTicket.getTicketNumber());
            //票种
            airETickets.setTicketType(airTicket.getTicketType());
            airETickets.setUser(user);
            airETickets.setStatus(0);
            AirETickets ticket = airETicketsDao.selectBySelfETickets(airETickets);
            if (ticket == null) {
                airETicketsDao.insert(airETickets);
            }
        }
    }

    /**
     * 查询多个电子票
     * @param eTicketsId
     * @return
     */
    @Override
    public List<AirETickets> selectByManyId(Long[] eTicketsId) {
        return airETicketsDao.selectByManyId(eTicketsId);
    }

    /**
     * 更新二维码ID
     * @param airETickets
     * @param airRideQrCode
     */
    @Override
    public void setQrCodeId(Long[] airETickets, AirRideQrCode airRideQrCode) {
        airETicketsDao.updateQrCodeId(airETickets, airRideQrCode.getId());
    }

    /**
     * 查询微信用户的openid
     * @param openid
     * @return
     */
    @Override
    public AirViewerUser selectByOpenidAndState(String openid) {
        return airViewerUserDao.selectByOpenidAndState(openid);
    }

    /**
     * 根据APP用户的username
     * @param username
     * @return
     */
    @Override
    public AirViewerUser selectByUsername(String username) {
        return airViewerUserDao.selectByUsername(username);
    }

    /**
     * 保存用户
     * @param currentUser
     */
    @Override
    public void save(AirViewerUser currentUser) {
        airViewerUserDao.insert(currentUser);
    }


    /**
     * 获取通知、公告信息
     * @param type 接收者的类型：1 代表观众，2 代表工作人员
     * @return
     */
    @Override
    public List<AirSendMessage> getMessageByReceiverType(int type) {
        return airSendMessageService.getMessageByReceiverType(type);
    }

    /**
     * 保存二维码
     * @param ridePoint
     * @param eTicketsId
     * @param bookingDate
     * @param phone
     * @param user
     * @return
     */
    @Override
    public AirRideQrCode save(Long ridePoint, Long[] eTicketsId, String bookingDate, String phone, AirViewerUser user) {
        AirRideQrCode airRideQrCode = new AirRideQrCode();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date currentDate = new Date();
        Date bDate = null;
        try {
            bDate = simpleDateFormat.parse(bookingDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        AirRidePoint airRidePoint = airRidePointService.get(ridePoint.intValue());
        airRideQrCode.setAirRidePoint(airRidePoint);
        airRideQrCode.setCreateDate(currentDate);
        airRideQrCode.setPhone(phone);
        airRideQrCode.setUser(user);
        airRideQrCode.setRideDate(bDate);
        airRideQrCode.setRideNo(eTicketsId.length);
        airRideQrCode.setRideStatus(-1);
        airRideQrCode.setRideType(1);
        if (airRidePoint.getRideType() == AirRidePoint.HOTEL_POINT){
            airRideQrCode.setRideType(AirRidePoint.HOTEL_POINT);
        }else if(airRidePoint.getRideType() == AirRidePoint.OTHER){
            airRideQrCode.setRideType(AirRidePoint.OTHER);
        }

        airRideQrCodeDao.insert(airRideQrCode);
        if (airRidePoint.getRideType() == 1){
            //生成二维码,日期需要修改
            bookingDate = bookingDate.replaceAll("\\.", "");
            String content = "2018airshow_zhits/date=" + bookingDate + "/no=" + eTicketsId.length + "/id=" + airRideQrCode.getId();
            //当前项目路径
            String realPath = servletContext.getRealPath("/");
            String path = realPath+FILEDIR+File.separator;
            System.out.println(path);
            String filename = null;
            try {
                System.out.println("picturepath:"+path);
                filename = QrCodeUtils.createQRCode(content, path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            airRideQrCode.setPictureUrl("/" + FILEDIR + "/" + filename);
            airRideQrCodeDao.update(airRideQrCode);
        }
        return airRideQrCode;
    }

    /**
     * 获取二维码
     * @param airRideQrCode
     * @return
     */
    @Override
    public AirRideQrCode get(AirRideQrCode airRideQrCode) {
        return airRideQrCodeDao.get(airRideQrCode.getId());
    }

    /**
     * 查询个人所有的二维码信息
     * @param id
     * @return
     */
    @Override
    public List<AirRideQrCode> selectByUserQRCodeId(Long id) {
        return airRideQrCodeDao.selectByUserId(id);
    }

    @Override
    public List<AirRideQrCode> selectByDate(String date) {
        return airRideQrCodeDao.selectByDate(date);
    }

    /**
     * 查询前台用户的消息
     * @return
     */
    @Override
    public List<AirSendMessage> SelectAirUserMessage() {
        return airSendMessageService.selectByAirUser();
    }


    @Override
    public List<AirTaxi> select20sGPS(Date currentDate, Date beforeDate){
        List<AirTaxi> taxiList = airTaxiDao.select20sGPS(currentDate, beforeDate);
        taxiList = GpsUtil.changetaxiListLngLat(taxiList);
        return taxiList;
    }

    /**
     * 根据车牌号查询对应的车辆信息
     * @param vehicleNo
     * @return
     */
    @Override
    public AirCar selectByCarNumber(String vehicleNo) {
        return airCarService.selectByCarNumber(vehicleNo);
    }

    /**
     * 判断生成的电子票是否在同一天
     * @param eTicketsIds
     */
    @Override
    public void isSameDay(Long[] eTicketsIds,String bookingDate) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date bookDate = dateFormat.parse(bookingDate);
        List<AirETickets> airETickets = airETicketsDao.selectByManyId(eTicketsIds);
        Date ticketDate = null;
        for (AirETickets airETicket : airETickets) {
            if(ticketDate == null){
                ticketDate = dateFormat.parse(airETicket.getTicketDate());
            }else{
                Date ticketDated = dateFormat.parse(airETicket.getTicketDate());
                if(!DateUtils.isSameDay(ticketDate,ticketDated)){
                    throw new ServiceException();
                }
            }
        }
        if(!DateUtils.isSameDay(ticketDate,bookDate)){
            throw new ServiceException();
        }
    }

    @Override
    public AirTickets selectByAirTickets(AirTickets airTickets){
        return airTicketsDao.selectByAirTickets(airTickets);
    }

    @Override
    public void saveTicket(AirTickets airTickets){
        airTicketsDao.insert(airTickets);
    }

    @Override
    public AirParkingLocation saveLocation(AirParkingLocation airParkingLocation) {
        airParkingLocationDao.insert(airParkingLocation);
        return airParkingLocation;
    }

    @Override
    public List<AirRealtimeMessage> getSubmission(String date) {
        return airRealtimeMessageService.getSubmission(date);
    }

    @Override
    public List<AirRealtimeMessage> getMessageByAirRidePointIdWithTime(Integer airRidePointId,String date) {
        return airRealtimeMessageService.getMessageByAirRidePointIdWithTime(airRidePointId,date);
    }

    @Override
    public void sendMessage(Integer airRidePointId, AirRealtimeMessage airRealtimeMessage, String carNumber) {
        airRealtimeMessageService.sendMessage(airRidePointId,airRealtimeMessage,carNumber);
    }

    @Override
    public Map<String, Integer> getMessageByAirRidePointId(Integer airRidePointId) {
        return airRealtimeMessageService.getMessageByAirRidePointId(airRidePointId);
    }

    @Override
    public List<AirRidePointVO> selectAllWaitingPoint() {
        return airRidePointService.findAllAppointList();
    }

    @Override
    public List<AirCar> getAllCarList() {
        return airCarService.findAllList(new AirCar());
    }

    @Override
    public List<AirCar> getAllPointsCar() {
        List<AirCar> allAirCarHavingGps = airCarService.getAllPointsCar();
        allAirCarHavingGps = GpsUtil.changeCarListLngLat(allAirCarHavingGps);
        return allAirCarHavingGps;
    }


    /**
     * 工作人员端登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public AirWorker workerLogin(String username, String password) {
        return airWorkerDao.login(username,password);
    }

    /**
     * 工作人员端首页
     * @param airWorker
     * @return
     */
    @Override
    public WorkerIndexVO workerIndex(AirWorker airWorker) {
        WorkerIndexVO workerIndexVO = new WorkerIndexVO();
        AirRidePoint airRidePoint = airWorker.getAirRidePoint();
        workerIndexVO.setPointName(airRidePoint.getName());
        workerIndexVO.setPosition(airRidePoint.getPosition());
//        查询该站点所有车辆
        List<AirCar> airCars = airCarService.selectByPointId(airRidePoint.getId());
        for (AirCar airCar : airCars) {
            if(airCar.getStatus() != null){
                if(airCar.getStatus() == AirCar.WAITSTART){
                    workerIndexVO.setReadyStartCarSum(workerIndexVO.getReadyStartCarSum()+1);
                }else if(airCar.getStatus() == AirCar.RIDING){
                    workerIndexVO.setRidingCarSum(workerIndexVO.getRidingCarSum()+1);
                }else if(airCar.getStatus() == AirCar.ARRIVE){
                    workerIndexVO.setArriveCarSum(workerIndexVO.getArriveCarSum()+1);
                }else if(airCar.getStatus() == AirCar.RETURNED){
                    workerIndexVO.setReturnedCarSum(workerIndexVO.getReturnedCarSum()+1);
                }
            }
        }
//        List<AirRealtimeMessage> messages = airRealtimeMessageDao.selectMessage4typeAndPointId(airRidePoint.getId(),AirRealtimeMessage.DEPARTURE, 10);
        List<AirRealtimeMessage> messages = airRealtimeMessageDao.getMessageItself(airRidePoint.getId());
        workerIndexVO.setMessages(messages);
        workerIndexVO.setAirRidePointId(airRidePoint.getId().longValue());
        return workerIndexVO;
    }

    private List<AirRealtimeMessage> spiltMessage(List<AirRealtimeMessage> messages) {
        List<AirRealtimeMessage> result = new ArrayList<>();
        for (AirRealtimeMessage message : messages) {
            if(message.getNote().indexOf("发车")>-1){
                result.add(message);
            }
        }
        return result;
    }

    /**
     * 更新车辆状态
     * @param airCarId
     * @param status
     */
    @Override
    public void updateCarStatus(Long airCarId, Integer status) {
        airCarService.updateCarStatus(airCarId,status);
    }

    /**
     * 保存车辆状态表信息
     * @param airWorker
     * @param airCarId
     * @param status
     * @param sum
     */
    @Override
    public void insertCarStartLog(AirWorker airWorker,Long airCarId, int status,int sum) {
        AirRidePoint airRidePoint = airWorker.getAirRidePoint();
        AirCarRecord airCarRecord = new AirCarRecord();
        AirCar airCar = new AirCar();
        airCar.setId(airCarId.intValue());
        airCarRecord.setRecordDate(new Date());
        airCarRecord.setStation(airRidePoint.getName());
        airCarRecord.setPassengerSum(sum);
        airCarRecord.setAirCar(airCar);
        airCarRecord.setStatus(status);
        airCarRecordDao.insert(airCarRecord);
    }

    /**
     * 保存发车人数的信息
     * @param workerUser
     * @param airCarId
     * @param riding
     * @param sum
     */
    @Override
    public void insertRealTimeMessage(AirWorker workerUser, Long airCarId, int riding, Integer sum) {
        AirCar airCar = airCarService.get(airCarId.intValue());
        saveRealTimeMessage(workerUser,AirRealtimeMessage.DEPARTURE,createNote(workerUser.getAirRidePoint().getName(),airCar.getCarNumber(),sum));
    }

    /**
     * 保存候车人数的信息
     * @param workerUser
     * @param sum
     */
    @Override
    public void insertWaitingRealTimeMessage(AirWorker workerUser, Integer sum) {
        saveRealTimeMessage(workerUser,AirRealtimeMessage.DEPARTURE,workerUser.getAirRidePoint().getName()+" 现场候车 "+sum+"人");
    }


    @Override
    public void insertTypeRealTimeMessage(AirWorker airWorker,Integer type, String message) {
        saveRealTimeMessage(airWorker,type,message);
    }

    /**
     * 更新乘车点候车人数
     * @param workerUser
     * @param sum
     */
    @Override
    public void updateWaitingSum(Integer pointId, Integer sum) {
        airRidePointService.updateWaitingSum(pointId,sum);
    }

    /**
     * 工作人员端获取的系统消息
     * @param userWorker
     * @return
     */
    @Override
    public List<AirSendMessage> getMessageByWorker(AirWorker userWorker) {
        return airSendMessageService.selectByPointId(2,userWorker.getAirRidePoint().getId());
    }

    @Override
    public List<AirSendMessage> getMessageByPointId(Integer pointId) {
        return airSendMessageService.selectByPointId(2,pointId);
    }

    /**
     * 保存车辆状态信息
     * @param car
     * @param riding
     */
    @Override
    public void insertCarRecord(AirCar car, int riding) {
        long currentTimeMillis = System.currentTimeMillis();
        AirCarRecord airCarRecord = new AirCarRecord();
        airCarRecord.setAirCar(car);
        airCarRecord.setStatus(riding);
        airCarRecord.setStation(car.getAirRidePoint().getName());
        airCarRecord.setPassengerSum(car.getSeating());
        if (car.getStatus() == AirCar.RETURNED && cn.mcsys.myadmin.common.utils.DateUtils.isInDate(currentTimeMillis, "07:00:00", "12:00:00")){
            airCarRecord.setPassengerSum(0);
        }else if(car.getStatus() == AirCar.RIDING && cn.mcsys.myadmin.common.utils.DateUtils.isInDate(currentTimeMillis, "13:00:00", "18:00:00")){
            airCarRecord.setPassengerSum(0);
        }else {
            airCarRecord.setPassengerSum(car.getSeating());
        }
        airCarRecord.setRecordDate(new Date());
        airCarRecordDao.insert(airCarRecord);
    }

    /**
     * 定时器插入发车记录
     * @param car
     * @param riding
     */
    @Override
    public void autoInsertRealTimeMessage(AirCar car, int riding) {
        baseSaveRealTimeMessage(AirRealtimeMessage.DEPARTURE,"系统",createNote(car.getAirRidePoint().getName(),car.getCarNumber(),car.getSeating()),car.getAirRidePoint());
    }

    @Override
    public List<AirCar> getAllCarsHavingGPS() {
        List<AirCar> carList = airCarService.getAllCarsHavingGPS();
        carList = GpsUtil.changeCarListLngLat(carList);
        return carList;
    }

    private void saveRealTimeMessage(AirWorker workerUser,Integer type,String note){
        baseSaveRealTimeMessage(type,workerUser.getName(),note,workerUser.getAirRidePoint());
    }

    private void baseSaveRealTimeMessage(int type,String name,String note,AirRidePoint point){
        String title = AirRealtimeMessage.chooseTitle(type);
        AirRealtimeMessage realtimeMessage = new AirRealtimeMessage();
        realtimeMessage.setAirRidePoint(point);
        if(type == AirRealtimeMessage.NOT_CAR || type == AirRealtimeMessage.CAR_TOO_MANY || type == AirRealtimeMessage.OTHER){
            type = AirRealtimeMessage.CAR_INSUFFICIENT;
        }
        realtimeMessage.setType(type);
        realtimeMessage.setFeedbackDate(new Date());
        realtimeMessage.setAirWorker(name);
        realtimeMessage.setTitle(title);
        realtimeMessage.setNote(note);
        airRealtimeMessageDao.insert(realtimeMessage);
    }

    private String createNote(String pointName,String carNumber,Integer sum){
        return pointName+" "+carNumber+" 已发车,载客"+sum+"人";
    }

    @Override
    public List<AirVehicleGpsBus> getBusGps(Date currentDate, Date beforeDate) {
        List<AirVehicleGpsBus> busList = airVehicleGpsBusDao.select20sGPS(currentDate, beforeDate);
        busList = GpsUtil.changeBusListLngLat(busList);
        return busList;
    }

    @Override
    public List<AirVehicleGpsTaxi> getTaxiGps(Date currentDate, Date beforeDate) {
        return airVehicleGpsTaxiDao.select20sGPS(currentDate,beforeDate);
    }

    @Override
    public List<AirCarRecord> getAirCarRecordToday() {
        return airCarRecordDao.getAirCarRecordToday();
    }

    @Override
    public AirCarRecord selectLatestCarRecord(AirCar airCar, Integer status) {
        return airCarRecordDao.selectLatestCarRecord(airCar.getId(),status);
    }

    @Override
    public AirCarRecord selectLatestCarRecordWithDate(AirCar airCar, int status, Date recordDate) {
        return airCarRecordDao.selectLatestCarRecordWithDate(airCar.getId(),status,recordDate);
    }

    @Override
    public Integer selectBookingSumByPointId(Integer pointId) {
        return airRideQrCodeDao.selectBookingSumByPointId(pointId);
    }

    @Override
    public List<AirTickets> selectTickets(Date begin, Date end) {
        return airTicketsDao.selectTickets(begin,end);
    }

    @Override
    public List<AirCarDTO> trafficList() {

        return airCarService.selectTrafficList();
    }

    @Override
    public List<AirCar> selectLikeByCarNumber(Integer pointId, String carNumber) {
        return airCarService.selectLikeByCarNumber(pointId,carNumber);
    }

    @Override
    public void batchInsertRealtimeUserCount(List<AirRealtimeUserCount> airRealtimeUserCounts) {
        airRealtimeUserCountDao.batchInsert(airRealtimeUserCounts);
    }

    @Override
    public void batchInsertUserFlowCount(List<AirUserFlowCount> airUserFlowCounts) {
        airUserFlowCountDao.batchInsert(airUserFlowCounts);
    }

    @Override
    public List<AirCar> getCarByStatusAndAirRidePointId(Integer status, Integer airRidePointId) {
        return airCarService.getCarByStatusAndAirRidePointId(status,airRidePointId);
    }

    @Override
    public List<AirETickets> selectAllETickets(int pageNo) {
        return airETicketsDao.selectAll(pageNo);
    }

    @Override
    public List<AirTickets> getAllTicketsToday() {
        return airTicketsDao.getAllTicketsToday();
    }

    @Override
    public List<AirRideQrCode> selectAllQrCode(int pageNo) {
        return airRideQrCodeDao.selectAll(pageNo);
    }

    @Override
    public List<AirTickets> selectAllTickets(int pageNo) {
        return airTicketsDao.selectAll(pageNo);
    }

    @Override
    public void UpdateExpiredETickets(List<Long> expiredETickets) {
        airETicketsDao.UpdateExpiredETickets(expiredETickets);
    }

    @Override
    public void UpdateExpiredQrCodes(List<Long> expiredETickets) {
        airRideQrCodeDao.UpdateExpiredQrCodes(expiredETickets);
    }

    @Override
    public void UpdateExpiredTickets(List<Long> expiredTickets) {
        airTicketsDao.UpdateExpiredTickets(expiredTickets);
    }

    @Override
    public void updateIsOnLine(Integer id,Integer isOnline) {
        airCarService.updateIsOnLine(id,isOnline);
    }

    @Override
    public void saveAirCarSumRecord(AirCarSumRecord airCarSumRecord) {
        airCarSumRecordDao.insert(airCarSumRecord);
    }

    @Override
    public List<AirCarSumRecord> getSumRecordWithTime(Date date) {
        return airCarSumRecordDao.getSumRecordWithTime(date);
    }

    @Override
    public void saveTaxiSumRecord(AirCarSumRecord airCarSumRecord) {
        airCarSumRecordDao.insert(airCarSumRecord);
    }

    @Override
    public List<AirTaxi> selectAllTaxi() {
        return airTaxiDao.selectAll();
    }

    @Override
    public AirCar getCarDetailByCarNumber(String carNumber) {
        AirCar car = airCarService.getDetailByCarNumber(carNumber);
        if(car!=null) {
            if (car.getLon() != null && car.getLat() != null) {
                double[] togcj02 = GpsUtil.wgs84togcj02(Double.valueOf(car.getLon()), Double.valueOf(car.getLat()));
                car.setLon(String.valueOf(togcj02[0]));
                car.setLat(String.valueOf(togcj02[1]));
            }
        }
        return car;
    }

    @Override
    public AirCar getCarDetailByCarId(Integer id) {
        AirCar car = airCarService.get(id);
        if(car!=null) {
            if(car.getLon()!=null) {
                double[] togcj02 = GpsUtil.wgs84togcj02(Double.valueOf(car.getLon()), Double.valueOf(car.getLat()));
                car.setLon(String.valueOf(togcj02[0]));
                car.setLat(String.valueOf(togcj02[1]));
            }
        }
        return car;
    }

    @Override
    public AirRidePoint getRidePointByName(String name) {
        return airRidePointDao.getRidePointByName(name);
    }

    @Override
    public Integer getRidePointSeatById(Integer id) {
        return airRidePointService.getSeatById(id);
    }

    @Override
    public List<AirWorker> getWorkersByRidePointId(Integer airRidePointId) {
        return airWorkerDao.getWorkersByRidePointId(airRidePointId);
    }

    @Override
    public List<AirCarRecord> getRecordTodayByPointId(Integer airRidePointId) {
        return airCarRecordDao.getRecordTodayByPointId(airRidePointId);
    }

    @Override
    public List<AirDriver> getDrivers() {
        return driverService.findAllDriver();
    }

    @Override
    public List<AirRealtimeMessage> getMessageLeast() {
        return airRealtimeMessageDao.getMessageLeast();
    }

    @Override
    public void cancelBooking(Long qrcodeId, Long id) {
//        查询出所有关联这二维码的电子票
        List<Long> airETicketIds = airETicketsService.selectByQrcodeId(qrcodeId);
//        设置二维码为删除标记
        System.out.println("更新DelFalg");
        airRideQrCodeDao.updateDelFlag(qrcodeId);
        System.out.println("更新电子票的qrId");
        airETicketsService.batchUpdateQrcodeId(airETicketIds);
    }

    @Override
    public void batchSaveTickets(List<AirTickets> airTicketsList) {
        airTicketsDao.batchSaveTickets(airTicketsList);
    }

    @Override
    public List<AirRealtimeUserCount> getUserCountsToday() {
        return airRealtimeUserCountDao.getUserCountsToday();
    }

    @Override
    public List<AirUserFlowCount> getUserFlowCountsToday() {
        return airUserFlowCountDao.getUserFlowCountsToday();
    }

    @Override
    public AirRidePoint selectByQrId4Point(Long qrcodeId) {
        AirRideQrCode airRideQrCode = airRideQrCodeDao.get(qrcodeId);
        return airRideQrCode.getAirRidePoint();
    }

    @Override
    public List<AirRidePoint> selectOutSidePoint() {
        return airRidePointDao.selectOutSidePoint();
    }

    @Override
    public List<AirRealtimeUserCount> getUserCountsLeast() {
        return airRealtimeUserCountDao.getUserCountsLeast();
    }

    @Override
    public void batchcreatepicture(List<AirRideQrCode> airRideQrCodes) {
        String realPath = servletContext.getRealPath("/");
        System.out.println(realPath);
        for (AirRideQrCode airRideQrCode : airRideQrCodes) {
            String pictureUrl = airRideQrCode.getPictureUrl();
            String substring = pictureUrl.substring(15);
            System.out.println("==========文件名字==============");
            System.out.println("文件名字:"+substring);
            String content = substring.replaceAll("-", "/");
            content = content.substring(0, content.indexOf("."));
            System.out.println("二维码内容："+content);
            String path = realPath+FILEDIR+File.separator;
            try {
                QrCodeUtils.createQRCode(content,path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询所有参与调度的车辆
     * @return
     */
    @Override
    public List<AirCar> selectZHAllCarList() {
        List<AirCar> carList = airCarService.selectZHAllCarList();
        carList = GpsUtil.changeCarListLngLat(carList);
        return carList;
    }

    @Override
    public List<AirVehicleGpsBus> selectAllBus() {
        return airVehicleGpsBusDao.selectAllCar();
    }

    @Override
    public List<AirVehicleGpsTaxi> selectAllTaxis() {
        return airVehicleGpsTaxiDao.selectAllCar();
    }

    @Override
    public List<AirCar> selectAllAirshowCar() {
        return airCarService.selectAllCars();
    }

    @Override
    public List<AirOutsideCar> selectAllOutSideCar() {
        return airOutsideCarDao.selectAllCars();
    }

    @Override
    public void updateWorkLonLat(AirWorker airWorker) {
        airWorkerDao.update(airWorker);
    }

    @Override
    public List<AirRidePoint> selectByPointType(Integer type) {
        return airRidePointService.selectByPointType(type);
    }

    @Override
    public AirRideQrCode bookingHotelPointAndGZABridge(AirViewerUser user, Integer pointId,String bookingDate, Long[] eTicketsIds, String phone) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
//        保存酒店/港珠澳大桥的二维码信息，但没有图片
        AirRideQrCode airRideQrCode = new AirRideQrCode();
        AirRidePoint airRidePoint = airRidePointService.get(pointId);
        airRideQrCode.setPhone(phone);
        airRideQrCode.setUser(user);
        try {
            airRideQrCode.setRideDate(format.parse(bookingDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        airRideQrCode.setAirRidePoint(airRidePoint);
        airRideQrCode.setCreateDate(new Date());
        airRideQrCode.setRideNo(eTicketsIds.length);
        if (airRidePoint.getRideType() == 3){
            airRideQrCode.setRideType(AirRidePoint.HOTEL_POINT);
        }else if(airRidePoint.getRideType() == 4){
            airRideQrCode.setRideType(AirRidePoint.OTHER);
        }
        airRideQrCodeDao.insert(airRideQrCode);
//        更改电子票的二维码ID
        airETicketsDao.updateQrCodeId(eTicketsIds,airRideQrCode.getId());
        return airRideQrCode;
    }

    @Override
    public List<AirRealtimeUserOrigin> getUserOriginLeast() {
        return airRealtimeUserOriginDao.getUserOriginLeast();
    }

    @Override
    public AirParkingLocation getParkingLocation(Long airParkingLocationId) {
        return airParkingLocationDao.get(airParkingLocationId);
    }

    @Override
    public List<AirRealtimeMessage> getMessageLeastByPointId(Integer airRidePointId) {
        return airRealtimeMessageDao.selectMessage4typeAndPointId(airRidePointId,4,100);
    }

    @Override
    public List<AirRideQrCodeSpecial> getQrCodeSpecialByNote(String note) {
        return airRideQrCodeSpecialDao.getQrCodeSpecialByNote(note);
    }

    @Override
    public List<AirRideQrCodeSpecial> getAllQrCodeSpecialToday() {
        return airRideQrCodeSpecialDao.getAllQrCodeSpecialToday();
    }

    @Override
    public List<AirRideQrCodeSpecial> selectByNotNull4SpecialCode() {
        return airRideQrCodeSpecialDao.selectByNotNull4SpecialCode();
    }

    @Override
    public void updateRideQrCodeSpecial(AirRideQrCodeSpecial airRideQrCodeSpecial) {
        airRideQrCodeSpecialDao.update(airRideQrCodeSpecial);
    }

    @Override
    public List<AirVehicleGpsBus> getBusByNote(String note) {
        List<AirVehicleGpsBus> busList = airVehicleGpsBusDao.getBusByNote(note);
        busList = GpsUtil.changeBusListLngLat(busList);
        return busList;
    }

    @Override
    public WorkerIndexVO workerVipIndex(Integer pointId) {
        WorkerIndexVO workerIndexVO = new WorkerIndexVO();
        AirRidePoint airRidePoint = airRidePointDao.get(pointId);
        workerIndexVO.setPointName(airRidePoint.getName());
        workerIndexVO.setPosition(airRidePoint.getPosition());
//        查询该站点所有车辆
        List<AirCar> airCars = airCarService.selectByPointId(airRidePoint.getId());
        for (AirCar airCar : airCars) {
            if(airCar.getStatus() != null){
                if(airCar.getStatus() == AirCar.WAITSTART){
                    workerIndexVO.setReadyStartCarSum(workerIndexVO.getReadyStartCarSum()+1);
                }else if(airCar.getStatus() == AirCar.RIDING){
                    workerIndexVO.setRidingCarSum(workerIndexVO.getRidingCarSum()+1);
                }else if(airCar.getStatus() == AirCar.ARRIVE){
                    workerIndexVO.setArriveCarSum(workerIndexVO.getArriveCarSum()+1);
                }else if(airCar.getStatus() == AirCar.RETURNED){
                    workerIndexVO.setReturnedCarSum(workerIndexVO.getReturnedCarSum()+1);
                }
            }
        }
        List<AirRealtimeMessage> messages = airRealtimeMessageDao.getMessageWithoutSys(pointId);
//        messages = spiltMessage(messages);
        workerIndexVO.setMessages(messages);
        workerIndexVO.setAirRidePointId(airRidePoint.getId().longValue());
        return workerIndexVO;
    }

    @Override
    public void clearCarStatus(Integer id) {
        airCarService.clearCarStatus(id);
    }

    @Override
    public Integer selectCountByPointId(Integer id, String date) {
        return airRideQrCodeDao.selectCountByPointId(id,date);
    }

    @Override
    public List<AirRideQrCodeSpecial> getAllQrCodeSpecialByDate(String note, String date) {
        return airRideQrCodeSpecialDao.getAllQrCodeSpecialByDate(note,date);
    }

    @Override
    public void batchInsertRealtimeUserOrigin(List<AirRealtimeUserOrigin> airRealtimeUserOrigins) {
        airRealtimeUserOriginDao.batchInsert(airRealtimeUserOrigins);
    }

    @Override
    public List<AirVehicleGpsBus> selectByShuttleBus() {
        return airVehicleGpsBusDao.selectByShuttleBus();
    }

    @Override
    public void insertBusRecord(AirVehicleGpsBus bus, int shuttlebustFinish) {
        AirShuttlebusRecord airShuttlebusRecord = new AirShuttlebusRecord();
        airShuttlebusRecord.setStatus(shuttlebustFinish);
        airShuttlebusRecord.setRecordDate(new Date());
        airShuttlebusRecord.setAirVehicleGpsBus(bus);
        airShuttlebusRecord.setStation("P4停车场");
        airShuttlebusRecordDao.insert(airShuttlebusRecord);
    }

    @Override
    public AirShuttlebusRecord selectShuttlebusRecord(Integer busId) {
        return airShuttlebusRecordDao.selectShuttlebusRecord(busId);
    }

    @Override
    public List<AirETickets> selectMyAllETickets(Long id) {
        return airETicketsService.selectMyAllETickets(id);
    }

    @Override
    public List<AirRideQrCode> selectAllQRCode() {
        return airRideQrCodeDao.selectAllQRCode();
    }

    @Override
    public Integer getAllQRCodeToday() {
        return airRideQrCodeDao.getAllCountToday();
    }

    @Override
    public List<AirRideQrCode> getQRCodeListByStatusToday(Integer status) {
        return airRideQrCodeDao.getByStatusToday(status);
    }

    @Override
    public List<AirCarRecord> getAirCarRecordLeast() {
        return airCarRecordDao.getAirCarRecordLeast();
    }

    @Override
    public List<AirCarRecord> getAirCarRecordInTime(String formatDate, Integer airRidePointId) {
        return airCarRecordDao.getAirCarRecordInTime(formatDate,airRidePointId);
    }

    @Override
    public List<AirCarRecord> getLeastRecordByStatusAndPointId(Integer airRidePointId, Integer status) {
        return airCarRecordDao.getLeastRecordByStatusAndPointId(airRidePointId,status);
    }
}
